//
//  NRWMySubscriptionVC.m
//  NRWMall
//
//  Created by 张记 on 2018/6/4.
//  Copyright © 2018年 com.anpxd. All rights reserved.
//

#import "NRWMySubscriptionVC.h"
#import "NRWCarListCell.h"
#import "NRWCarDetailVC.h"
#import "NRWFliterAutoLabelView.h"
#import "NRWSubscribeInfoApi.h"
#import "NRWSubScribeListApi.h"
#import "NRWSubscribeInfoModel.h"
//#import "NRWBuyCarListDataModel.h"
#import "NRWFilterScrollModel.h"
#import "NRWSubscribeVC.h"
#import "NRWBuyCarListRectModel.h"

@interface NRWMySubscriptionVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableview;
@property(nonatomic, strong) NRWFliterAutoLabelView *fliterView;
@property(nonatomic, strong) NRWSubscribeInfoApi *infoApi;
@property(nonatomic, strong) NRWSubScribeListApi *listApi;
@property(nonatomic, strong) NRWSubscribeInfoModel *infoModel;
//@property(nonatomic, strong) NRWBuyCarListDataModel *carListModel;
//@property(nonatomic, strong) NSMutableArray *carListArray;
@property (strong, nonatomic) NSArray<NRWBuyCarListRectModel *> *carListArr;
@property(nonatomic, strong) NSMutableArray *templateArray;
@end

@implementation NRWMySubscriptionVC

-(NRWSubscribeInfoApi *)infoApi{
    if (!_infoApi) {
        _infoApi = [[NRWSubscribeInfoApi alloc] init];
        _infoApi.delegate = self;
    }
    return _infoApi;
}
-(NRWSubScribeListApi *)listApi{
    if (!_listApi) {
        _listApi = [[NRWSubScribeListApi alloc] init];
        _listApi.delegate = self;
    }
    return _listApi;
}

-(NSMutableArray *)templateArray{
    if (!_templateArray) {
        _templateArray = [[NSMutableArray alloc] init];
    }
    return _templateArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationView setTitle:@"我的订阅"];
    [self.view addSubview:self.fliterView];
    [self initTableview];
    
    UIButton *subscribeBtn = [[UIButton alloc] init];
    [subscribeBtn setImage:getImage(@"home_subscribe") forState:UIControlStateNormal];
    [subscribeBtn addTarget:self action:@selector(subscribeBtnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    subscribeBtn.frame = CGRectMake(SCREEN_WIDTH() - 75 - 12, SCREEN_HEIGHT() - getTabBarHeight() - 75 - 32, 75, 75);
    [self.view addSubview:subscribeBtn];
    [self.view bringSubviewToFront:subscribeBtn];
}
-(void)subscribeBtnDidClick:(UIButton *)sender{

    NRWSubscribeVC *vc = [[NRWSubscribeVC alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self reloadData];

}
-(void)reloadData{
    [self.infoApi start];
    self.listApi.pageIndex = 1;
    [self.listApi start];
}
#pragma mark - setupUI

-(NRWFliterAutoLabelView *)fliterView{
    if (!_fliterView) {
        _fliterView = [[NRWFliterAutoLabelView alloc] initWithFrame:CGRectMake(0, getNavHeight(), SCREEN_WIDTH(), 50)];
        _fliterView.isShowClose = YES;
    }
    return _fliterView;
}
-(void)initTableview{
    kSelfWeak;
    self.tableview = [[UITableView alloc] init];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = backColor();
    [self.tableview registerClass:[NRWCarListCell class] forCellReuseIdentifier:@"NRWCarListCell"];
    self.tableview.tableFooterView = DPView();
    self.tableview.estimatedRowHeight = 0;
    self.tableview.estimatedSectionHeaderHeight = 0;
    self.tableview.estimatedSectionFooterHeight = 0;
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).mas_offset(getNavHeight() + 50);
        make.left.right.bottom.equalTo(weakSelf.view);
        
    }];
    if (@available(iOS 11.0, *)) {
        self.tableview.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    self.tableview.mj_footer = footer;
}
-(void)loadMoreData{
    self.listApi.pageIndex ++;
    [self.listApi start];
}


#pragma mark - TableviewDelegateAndSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.carListArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NRWCarListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NRWCarListCell" forIndexPath:indexPath];
    cell.type = cellSubscribeType;
    cell.carFrameModel = self.carListArr[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	
    Result *rt = self.carListArr[indexPath.row].dataModel;
    NRWCarDetailVC *DetailVc = [[NRWCarDetailVC alloc] init];
    DetailVc.carId = rt.carId;
    DetailVc.thisCarModel = rt;
    [self.navigationController pushViewController:DetailVc animated:YES];
    
}
- (void)routerEventWithName:(NSString *)eventName userInfo:(NSDictionary *)userInfo{
    NSIndexPath *index = userInfo[@"index"];
    if ([eventName isEqualToString:@"Close"]) {
        
    }
}

- (void)requestFinished:(__kindof YTKBaseRequest *)request {
    if (request == self.infoApi) {
        self.infoModel = [NRWSubscribeInfoModel mj_objectWithKeyValues:request.responseObject];
        [self templateTitleData:self.infoModel.result];

    }else if (request == self.listApi){
        if (self.listApi.pageIndex == 1) {
            self.carListArr = nil;
        }
        NRWBuyCarListDataModel *carListModel = [NRWBuyCarListDataModel mj_objectWithKeyValues:request.responseObject];
		NSMutableArray *arrM = [NSMutableArray arrayWithArray:self.carListArr];
		for (Result *itemModel in carListModel.result) {
			NRWBuyCarListRectModel *rectModel = [NRWBuyCarListRectModel new];
			rectModel.dataModel = itemModel;
			[arrM addObject:rectModel];
		}
		self.carListArr = arrM.copy;
		
        [self.tableview reloadData];
        [self.tableview.mj_footer endRefreshing];
        if (self.carListArr.count == 0) {
            kSelfWeak;
            [self.tableview showEmptyMaskViewWithClickBlock:^{
                weakSelf.listApi.pageIndex = 1;
                [weakSelf.listApi start];
            }];
        }else{
            [self.tableview removeMaskView];
        }
    }
    
}
-(void)templateTitleData:(NRWInfoModel *)infoModel{
    [self.templateArray removeAllObjects];
    if (isEmptyString(infoModel.templateSeriesName) == NO) {
        NRWFilterScrollModel *filterModel = [[NRWFilterScrollModel alloc] init];
        filterModel.title = infoModel.templateSeriesName;
        [self.templateArray addObject:filterModel];
    }
    if (![infoModel.templateCarAgeTitle isEqualToString:@"不限"]) {
        NRWFilterScrollModel *filterModel = [[NRWFilterScrollModel alloc] init];
        filterModel.title = infoModel.templateCarAgeTitle;
        [self.templateArray addObject:filterModel];
    }
    if (![infoModel.templatePriceTitle isEqualToString:@"不限"]) {
        NRWFilterScrollModel *filterModel = [[NRWFilterScrollModel alloc] init];
        filterModel.title = infoModel.templatePriceTitle;
        [self.templateArray addObject:filterModel];
    }
    if (![infoModel.templateMileageTitle isEqualToString:@"不限"]) {
        NRWFilterScrollModel *filterModel = [[NRWFilterScrollModel alloc] init];
        filterModel.title = infoModel.templateMileageTitle;
        [self.templateArray addObject:filterModel];
    }
    if (isEmptyString(infoModel.templateCarTypeName) == NO) {
        NRWFilterScrollModel *filterModel = [[NRWFilterScrollModel alloc] init];
        filterModel.title = infoModel.templateCarTypeName;
        [self.templateArray addObject:filterModel];
    }
    if (isEmptyString(infoModel.templateCityName) == NO) {
        NRWFilterScrollModel *filterModel = [[NRWFilterScrollModel alloc] init];
        filterModel.title = infoModel.templateCityName;
        [self.templateArray addObject:filterModel];
    }
    if (isEmptyString(infoModel.templateConfigName) == NO) {
        
        NSArray *titles = [infoModel.templateConfigName componentsSeparatedByString:@","];
        for (NSString *str in titles) {
            NRWFilterScrollModel *filterModel = [[NRWFilterScrollModel alloc] init];
            filterModel.title = str;
            [self.templateArray addObject:filterModel];
        }
    }
    if (isEmptyString(infoModel.templateEnviron) == NO) {
        NRWFilterScrollModel *filterModel = [[NRWFilterScrollModel alloc] init];
        filterModel.title = infoModel.templateEnviron;
        [self.templateArray addObject:filterModel];
    }
    if (isEmptyString(infoModel.templateGearbox) == NO) {
        NRWFilterScrollModel *filterModel = [[NRWFilterScrollModel alloc] init];
        filterModel.title = infoModel.templateGearbox;
        [self.templateArray addObject:filterModel];
    }
    if (isEmptyString(infoModel.templateOutColor) == NO) {
        NRWFilterScrollModel *filterModel = [[NRWFilterScrollModel alloc] init];
        filterModel.title = infoModel.templateOutColor;
        [self.templateArray addObject:filterModel];
    }
    kSelfWeak;
    if (self.templateArray.count == 0) {
        [self.tableview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.view).mas_offset(getNavHeight());
        }];
    }else{
        [self.tableview mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).mas_offset(getNavHeight() + 50);
        }];
    }
    self.fliterView.modelArray = self.templateArray.copy;
    
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request {
    [self.tableview.mj_footer endRefreshing];
    [NRWMessageHUD showMessage:@"请求失败" forTime:2.0 withRemindId:remindFail];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
